#include "AEStateManager.hpp"
#include "AELogManager.hpp"

using namespace arete::components;
using namespace arete::io;

BEGIN_ARETE_NAMESPACE(core)

	AEStateManager::AEStateManager(void)
{
}

AEStateManager::~AEStateManager(void)
{
	m_inactive_states.clear();
	m_active_state.second.reset();
}

void AEStateManager::initialize(void)
{
	AELogManager::log(ELL_NONE,"*****************************************");
	AELogManager::log(ELL_NONE,"*** --- Initializing StateManager --- ***");
	AELogManager::log(ELL_NONE,"*****************************************");
}

bool AEStateManager::createState(std::shared_ptr<AEState> _state,const string &_name)
{
	if(m_inactive_states.find(_name) == m_inactive_states.end())
	{
		m_inactive_states[_name] = _state;
	}

	return false;
}

bool AEStateManager::setActive(const string &_state)
{
	Ogre::map<string,std::shared_ptr<AEState>>::iterator iter = m_inactive_states.find(_state);
	if(iter != m_inactive_states.end())
	{
		if(m_active_state.first != "")
		{
			m_active_state.second->setDeactive();
			m_inactive_states[m_active_state.first] = m_active_state.second;
		}

		m_active_state.first = iter->first;
		m_active_state.second = iter->second;
		m_active_state.second->setActive();

		m_inactive_states.erase(iter);

		return true;
	}

	return false;
}

bool AEStateManager:: removeState(const string &_state)
{
	Ogre::map<string,std::shared_ptr<AEState>>::iterator iter = m_inactive_states.find(_state);
	if(iter != m_inactive_states.end())
	{
		m_inactive_states.erase(iter);
		return true;
	}

	return false;
}

AEState* AEStateManager::getActiveState(void)
{
	return m_active_state.second.get();
}

AEState* AEStateManager::getState(const string& _state)
{
	Ogre::map<string,std::shared_ptr<AEState>>::iterator iter = m_inactive_states.find(_state);
	if(iter != m_inactive_states.end()) {
		return iter->second.get();
	} else {
		return NULL;
	}
}

END_ARETE_NAMESPACE